package com.xuecheng.content.controller;

import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController

public class CourseBaseController {

    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    private CoursePubService coursePubService;

    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params,
                                  @RequestBody QueryCourseModel model) {
        //1.获取访问令牌并从中解析出机构的信息id数据
        Long companyId = SecurityUtil.getCompanyId();
        //2.调用service层方法
        PageVO<CourseBaseDTO> pageVO = courseBaseService.queryCourseBaselist(params, model, companyId);
        return pageVO;
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        //通过工具类获得公司和用户信息
        Long companyId = SecurityUtil.getCompanyId();
        //设置公司和登录用户信息
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.createCourseBase(courseBaseDTO);
    }

    @GetMapping(value = "course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        //1.获得公司Id值
        Long companyId = SecurityUtil.getCompanyId();
        //2.获得课程基本信息数据
        CourseBaseDTO courseBase = courseBaseService.getCourseBaseById(courseBaseId, companyId);
        return courseBase;
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //1.将VO数据转为DTO数据
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        //2.通过工具类获得公司和用户信息
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.modifyCourseBase(courseBaseDTO);

    }

    @DeleteMapping("course/{courseBaseId}")
    public void removeCourseBaseById(@PathVariable Long courseBaseId) {
        //获取公司id
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.removeCourseBase(courseBaseId, companyId);

    }

    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }


        /*
         * SpringMVC 中controller 多结果视图器并存
         *      CourseBaseController ：
         *           JackSonResolver （默认）--controller使用的是RestController
         *           FreemarkerViewResolver
         *               1.添加依赖：spring-boot-starter-Freemarker
         *               2.添加配置：spring.freemarker.enabled = true
         *               3.方法的返回：controller的方法返回为ModelAndView对象
         * */
        @GetMapping("course/preview/{courseId}/{companyId}")
        public Object previewCourse(@PathVariable Long courseId,@PathVariable Long companyId) {

            Map<String, Object> dataMap = coursePubService.previewCourse(courseId, companyId);

            // 1.使用视图和数据模型对象
            ModelAndView modelAndView = new ModelAndView("learing_article",dataMap);

            return modelAndView;
        }

}
